﻿using Knife.Core.Model;
using Knife.Core.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Knife.Core.DataBase
{
    public class CommonExecute
    {
        public static void Insert<T>(T entity) where T : BaseEntity
        {
            entity.row_id = CommonUtil.GetNewGuid();
            entity.gmt_create = DateTime.Now;
            entity.gmt_modified = DateTime.Now;
            string sql = SQLBuilder.BuildInsertSQL<T>();
            DapperHelper.ExecuteNonQuery(sql, entity);
        }
        public static void Delete<T>(string row_id) where T : BaseEntity
        {
            string sql = SQLBuilder.BuildDeleteSQL<T>();
            Dictionary<string, object> paramList = new Dictionary<string, object>();
            paramList.Add("row_id", row_id);
            DapperHelper.ExecuteNonQuery(sql, paramList);
        }
        public static void Update<T>(T entity) where T : BaseEntity
        {
            entity.gmt_modified = DateTime.Now;
            string sql = SQLBuilder.BuildUpdateSQL<T>();
            DapperHelper.ExecuteNonQuery(sql, entity);
        }
        public static int ExecuteSQL(string sql, Dictionary<string, object> paramList = null)
        {
            return DapperHelper.ExecuteNonQuery(sql, paramList);
        }
        public static object ExecuteScar(string sql, Dictionary<string, object> paramList = null)
        {
            return DapperHelper.ExecuteScalar<object>(sql, paramList);
        }
        public static int ExecuteInt(string sql, Dictionary<string, object> paramList = null)
        {
            return DapperHelper.ExecuteScalar<int>(sql, paramList);
        }
        public static string ExecuteString(string sql, Dictionary<string, object> paramList = null)
        {
            return DapperHelper.ExecuteScalar<string>(sql, paramList);
        }
    }
}
